#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
    {
    }
};

class Solution 
{
    TreeNode * createNode(int val)
    {
        return new TreeNode(val);
    }
    TreeNode * recursion(vector<int> &nums, int begin, int end)
    {
        if(begin >= end)
        {
            return nullptr;
        }
        int mid = (begin + end) / 2;
        auto node = createNode(nums[mid]);
        node->left = recursion(nums, begin, mid);
        node->right = recursion(nums, mid + 1, end);
        return node;
    }
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) 
    {
        // 左闭右开
        int begin = 0;
        int end = nums.size();
        return recursion(nums, begin, end);
    }
};

// 辅助函数：层序遍历打印二叉树
void printTree(TreeNode *root)
{
    if (!root)
    {
        cout << "[]" << endl;
        return;
    }

    queue<TreeNode *> q;
    q.push(root);
    vector<string> result;

    while (!q.empty())
    {
        TreeNode *node = q.front();
        q.pop();

        if (node)
        {
            result.push_back(to_string(node->val));
            q.push(node->left);
            q.push(node->right);
        } else
        {
            result.push_back("null");
        }
    }

    // 移除末尾的null
    while (!result.empty() && result.back() == "null")
    {
        result.pop_back();
    }

    cout << "[";
    for (int i = 0; i < result.size(); i++)
    {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：中序遍历验证BST性质
void inorderTraversal(TreeNode *root, vector<int> &result)
{
    if (!root) return;
    inorderTraversal(root->left, result);
    result.push_back(root->val);
    inorderTraversal(root->right, result);
}

// 辅助函数：验证是否为有效的BST
bool isValidBST(TreeNode *root)
{
    vector<int> inorder;
    inorderTraversal(root, inorder);
    for (int i = 1; i < inorder.size(); i++)
    {
        if (inorder[i] <= inorder[i-1]) return false;
    }
    return true;
}

// 辅助函数：计算树的高度
int getHeight(TreeNode *root)
{
    if (!root) return 0;
    return 1 + max(getHeight(root->left), getHeight(root->right));
}

// 辅助函数：验证是否为平衡二叉树
bool isBalanced(TreeNode *root)
{
    if (!root) return true;
    
    int leftHeight = getHeight(root->left);
    int rightHeight = getHeight(root->right);
    
    return abs(leftHeight - rightHeight) <= 1 && 
           isBalanced(root->left) && 
           isBalanced(root->right);
}

// 辅助函数：验证中序遍历结果是否与原数组相同
bool isSameAsOriginal(TreeNode *root, vector<int> &nums)
{
    vector<int> inorder;
    inorderTraversal(root, inorder);
    return inorder == nums;
}

// 辅助函数：打印vector
void printVector(const vector<int> &vec)
{
    cout << "[";
    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec[i];
        if (i < vec.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode *root)
{
    if (!root) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main()
{
    Solution solution;

    cout << "=== LeetCode 108. 将有序数组转换为二叉搜索树 测试案例 ===" << endl << endl;

    // 测试案例1：题目示例1
    cout << "测试案例1：" << endl;
    vector<int> nums1 = {-10, -3, 0, 5, 9};
    cout << "输入: nums = ";
    printVector(nums1);

    TreeNode *result1 = solution.sortedArrayToBST(nums1);
    cout << "输出: ";
    printTree(result1);
    cout << "验证BST性质: " << (isValidBST(result1) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result1) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result1, nums1) ? "通过" : "失败") << endl << endl;

    // 测试案例2：题目示例2
    cout << "测试案例2：" << endl;
    vector<int> nums2 = {1, 3};
    cout << "输入: nums = ";
    printVector(nums2);

    TreeNode *result2 = solution.sortedArrayToBST(nums2);
    cout << "输出: ";
    printTree(result2);
    cout << "验证BST性质: " << (isValidBST(result2) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result2) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result2, nums2) ? "通过" : "失败") << endl << endl;

    // 测试案例3：单个元素
    cout << "测试案例3（单个元素）：" << endl;
    vector<int> nums3 = {0};
    cout << "输入: nums = ";
    printVector(nums3);

    TreeNode *result3 = solution.sortedArrayToBST(nums3);
    cout << "输出: ";
    printTree(result3);
    cout << "验证BST性质: " << (isValidBST(result3) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result3) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result3, nums3) ? "通过" : "失败") << endl << endl;

    // 测试案例4：两个元素
    cout << "测试案例4（两个元素）：" << endl;
    vector<int> nums4 = {1, 2};
    cout << "输入: nums = ";
    printVector(nums4);

    TreeNode *result4 = solution.sortedArrayToBST(nums4);
    cout << "输出: ";
    printTree(result4);
    cout << "验证BST性质: " << (isValidBST(result4) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result4) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result4, nums4) ? "通过" : "失败") << endl << endl;

    // 测试案例5：较长的数组
    cout << "测试案例5（较长的数组）：" << endl;
    vector<int> nums5 = {1, 2, 3, 4, 5, 6, 7};
    cout << "输入: nums = ";
    printVector(nums5);

    TreeNode *result5 = solution.sortedArrayToBST(nums5);
    cout << "输出: ";
    printTree(result5);
    cout << "验证BST性质: " << (isValidBST(result5) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result5) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result5, nums5) ? "通过" : "失败") << endl << endl;

    // 测试案例6：包含负数的数组
    cout << "测试案例6（包含负数）：" << endl;
    vector<int> nums6 = {-5, -3, -1, 0, 1, 3, 5};
    cout << "输入: nums = ";
    printVector(nums6);

    TreeNode *result6 = solution.sortedArrayToBST(nums6);
    cout << "输出: ";
    printTree(result6);
    cout << "验证BST性质: " << (isValidBST(result6) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result6) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result6, nums6) ? "通过" : "失败") << endl << endl;

    // 测试案例7：偶数个元素
    cout << "测试案例7（偶数个元素）：" << endl;
    vector<int> nums7 = {1, 2, 3, 4, 5, 6};
    cout << "输入: nums = ";
    printVector(nums7);

    TreeNode *result7 = solution.sortedArrayToBST(nums7);
    cout << "输出: ";
    printTree(result7);
    cout << "验证BST性质: " << (isValidBST(result7) ? "通过" : "失败") << endl;
    cout << "验证平衡性: " << (isBalanced(result7) ? "通过" : "失败") << endl;
    cout << "验证中序遍历: " << (isSameAsOriginal(result7, nums7) ? "通过" : "失败") << endl << endl;

    // 清理内存
    deleteTree(result1);
    deleteTree(result2);
    deleteTree(result3);
    deleteTree(result4);
    deleteTree(result5);
    deleteTree(result6);
    deleteTree(result7);

    cout << "所有测试案例执行完毕！" << endl;

    return 0;
}